<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>bidiinput.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_bidiinput.js.html">bidiinput.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Component for an input field with bidi direction automatic
<a name="line17"></a> * detection. The input element directionality is automatically set according
<a name="line18"></a> * to the contents (value) of the element.
<a name="line19"></a> *
<a name="line20"></a> * @see ../demos/bidiinput.html
<a name="line21"></a> */
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>goog.provide(&#39;goog.ui.BidiInput&#39;);
<a name="line25"></a>
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.events&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.InputHandler&#39;);
<a name="line29"></a>goog.require(&#39;goog.i18n.bidi&#39;);
<a name="line30"></a>goog.require(&#39;goog.ui.Component&#39;);
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>
<a name="line34"></a>/**
<a name="line35"></a> * Default implementation of BidiInput.
<a name="line36"></a> *
<a name="line37"></a> * @param {goog.dom.DomHelper=} opt_domHelper  Optional DOM helper.
<a name="line38"></a> * @constructor
<a name="line39"></a> * @extends {goog.ui.Component}
<a name="line40"></a> */
<a name="line41"></a>goog.ui.BidiInput = function(opt_domHelper) {
<a name="line42"></a>  goog.ui.Component.call(this, opt_domHelper);
<a name="line43"></a>};
<a name="line44"></a>goog.inherits(goog.ui.BidiInput, goog.ui.Component);
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * The input handler that provides the input event.
<a name="line49"></a> * @type {goog.events.InputHandler?}
<a name="line50"></a> * @private
<a name="line51"></a> */
<a name="line52"></a>goog.ui.BidiInput.prototype.inputHandler_ = null;
<a name="line53"></a>
<a name="line54"></a>
<a name="line55"></a>/**
<a name="line56"></a> * Decorates the given HTML element as a BidiInput. The HTML element
<a name="line57"></a> * must be an input element with type=&#39;text&#39; or a textarea element.
<a name="line58"></a> * Overrides {@link goog.ui.Component#decorateInternal}.  Considered protected.
<a name="line59"></a> * @param {Element} element  Element (HTML Input element) to decorate.
<a name="line60"></a> * @protected
<a name="line61"></a> * @override
<a name="line62"></a> */
<a name="line63"></a>goog.ui.BidiInput.prototype.decorateInternal = function(element) {
<a name="line64"></a>  goog.ui.BidiInput.superClass_.decorateInternal.call(this, element);
<a name="line65"></a>  this.init_();
<a name="line66"></a>};
<a name="line67"></a>
<a name="line68"></a>
<a name="line69"></a>/**
<a name="line70"></a> * Creates the element for the text input.
<a name="line71"></a> * @protected
<a name="line72"></a> * @override
<a name="line73"></a> */
<a name="line74"></a>goog.ui.BidiInput.prototype.createDom = function() {
<a name="line75"></a>  this.setElementInternal(
<a name="line76"></a>      this.getDomHelper().createDom(&#39;input&#39;, {&#39;type&#39;: &#39;text&#39;}));
<a name="line77"></a>  this.init_();
<a name="line78"></a>};
<a name="line79"></a>
<a name="line80"></a>
<a name="line81"></a>/**
<a name="line82"></a> * Initializes the events and initial text direction.
<a name="line83"></a> * Called from either decorate or createDom, after the input field has
<a name="line84"></a> * been created.
<a name="line85"></a> * @private
<a name="line86"></a> */
<a name="line87"></a>goog.ui.BidiInput.prototype.init_ = function() {
<a name="line88"></a>  // Set initial direction by current text
<a name="line89"></a>  this.setDirection_();
<a name="line90"></a>
<a name="line91"></a>  // Listen to value change events
<a name="line92"></a>  this.inputHandler_ = new goog.events.InputHandler(this.getElement());
<a name="line93"></a>  goog.events.listen(this.inputHandler_,
<a name="line94"></a>      goog.events.InputHandler.EventType.INPUT,
<a name="line95"></a>      this.setDirection_, false, this);
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Set the direction of the input element based on the current value. If the
<a name="line101"></a> * value does not have any strongly directional characters, remove the dir
<a name="line102"></a> * attribute so that the direction is inherited instead.
<a name="line103"></a> * This method is called when the user changes the input element value, or
<a name="line104"></a> * when a program changes the value using
<a name="line105"></a> * {@link goog.ui.BidiInput#setValue}
<a name="line106"></a> * @private
<a name="line107"></a> */
<a name="line108"></a>goog.ui.BidiInput.prototype.setDirection_ = function() {
<a name="line109"></a>  var element = this.getElement();
<a name="line110"></a>  var text = element.value;
<a name="line111"></a>  switch (goog.i18n.bidi.estimateDirection(text)) {
<a name="line112"></a>    case (goog.i18n.bidi.Dir.LTR):
<a name="line113"></a>      element.dir = &#39;ltr&#39;;
<a name="line114"></a>      break;
<a name="line115"></a>    case (goog.i18n.bidi.Dir.RTL):
<a name="line116"></a>      element.dir = &#39;rtl&#39;;
<a name="line117"></a>      break;
<a name="line118"></a>    default:
<a name="line119"></a>      // Default for no direction, inherit from document.
<a name="line120"></a>      element.removeAttribute(&#39;dir&#39;);
<a name="line121"></a>  }
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Returns the direction of the input element.
<a name="line127"></a> * @return {?string} Return &#39;rtl&#39; for right-to-left text,
<a name="line128"></a> *     &#39;ltr&#39; for left-to-right text, or null if the value itself is not
<a name="line129"></a> *     enough to determine directionality (e.g. an empty value), and the
<a name="line130"></a> *     direction is inherited from a parent element (typically the body
<a name="line131"></a> *     element).
<a name="line132"></a> */
<a name="line133"></a>goog.ui.BidiInput.prototype.getDirection = function() {
<a name="line134"></a>  var dir = this.getElement().dir;
<a name="line135"></a>  if (dir == &#39;&#39;) {
<a name="line136"></a>    dir = null;
<a name="line137"></a>  }
<a name="line138"></a>  return dir;
<a name="line139"></a>};
<a name="line140"></a>
<a name="line141"></a>
<a name="line142"></a>/**
<a name="line143"></a> * Sets the value of the underlying input field, and sets the direction
<a name="line144"></a> * according to the given value.
<a name="line145"></a> * @param {string} value  The Value to set in the underlying input field.
<a name="line146"></a> */
<a name="line147"></a>goog.ui.BidiInput.prototype.setValue = function(value) {
<a name="line148"></a>  this.getElement().value = value;
<a name="line149"></a>  this.setDirection_();
<a name="line150"></a>};
<a name="line151"></a>
<a name="line152"></a>
<a name="line153"></a>/**
<a name="line154"></a> * Returns the value of the underlying input field.
<a name="line155"></a> * @return {string} Value of the underlying input field.
<a name="line156"></a> */
<a name="line157"></a>goog.ui.BidiInput.prototype.getValue = function() {
<a name="line158"></a>  return this.getElement().value;
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/** @override */
<a name="line163"></a>goog.ui.BidiInput.prototype.disposeInternal = function() {
<a name="line164"></a>  if (this.inputHandler_) {
<a name="line165"></a>    goog.events.removeAll(this.inputHandler_);
<a name="line166"></a>    this.inputHandler_.dispose();
<a name="line167"></a>    this.inputHandler_ = null;
<a name="line168"></a>    goog.ui.BidiInput.superClass_.disposeInternal.call(this);
<a name="line169"></a>  }
<a name="line170"></a>};
</pre>


</body>
</html>
